angular.module("carPoolingApp",['ui.bootstrap','httpService']).
controller("carPoolingCtrl",['$scope','$http','$uibModal','$timeout','carPoolingService',function($scope,$http,$modal,$timeout,carPoolingService){ 
    $scope.addNotice=function(){
        $modal.open({
            templateUrl:"modifyNotice",
            controller:"insertNoticeCtrl"
        });
    };
    $scope.updateNotice=function(){
        if($scope.checkedIndex==-1){
        alert("请选择");
        }
         $modal.open({
            templateUrl:"modifyNotice",
            scope:$scope,
            controller:"updateNoticeCtrl"
        });
    };
    $scope.deleteNotice=function(){
         if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
        var tmp=window.confirm("确认是否删除");
        if (tmp!=true) {
            return;
            };
        carPoolingService.delete({id:$scope.notices[$scope.checkedIndex].id}).then(function(response){          
            $scope.refresh();
            alert("删除成功");
        });
    };
    $scope.refresh=function(){
        carPoolingService.getValidateCarPooling().then(function(response){
        $scope.notices=response;
    },function(response){
       console.log("获取数据失败");
    });
    };
    $scope.refresh();
    $scope.checkedIndex = -1;
    $scope.selectChange=function(index){         
       if($scope.notices[index].checked==true){
            $scope.notices[index].checked=false;
          }
       else{
            $scope.notices[index].checked=true;
           }
        if($scope.notices[index].checked){
            $scope.checkedIndex = index;
            return;
        }
        for(var i = 0; i < $scope.notices.length; i++){
            var item = $scope.notices[i];
            if(item.checked != undefined && item.checked == true){
                $scope.checkedIndex = i;
                return;
            }
        }
        $scope.checkedIndex = -1;
    };
}])
.controller('insertNoticeCtrl',["$scope",'$uibModalInstance',"carPoolingService",function($scope,$uibModalInstance,carPoolingServicebaseUrl){
    $scope.close=function(){
        $uibModalInstance.close();
    };
     $scope.submit=function(){
        delete $scope.notice['checked'];
        $scope.loading=true;
        carPoolingServicebaseUrl.insert($scope.notice).then(function(result){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="成功";
        });
    };
}])
.controller('updateNoticeCtrl',["$scope",'$uibModalInstance',"carPoolingService",function($scope,$uibModalInstance,carPoolingServicebaseUrl){
   
    $scope.notice=$scope.notices[$scope.checkedIndex];
    $scope.close=function(){
        $uibModalInstance.close();
    };
    $scope.submit=function(){
        delete $scope.notice['checked'];
        $scope.loading=true;
        carPoolingServicebaseUrl.update($scope.notice).then(function(result){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="成功";
        });
    };
}]);